﻿using System;
using System.Collections.Generic;

namespace MyLib.Data {
	/// <summary>
	/// ActionTable
	/// </summary>
	[System.ComponentModel.DesignerCategory("Code")]
	public class ActionTable : System.Data.DataTable, System.Collections.Generic.IList<ActionRow> {
		/// <summary>
		/// 
		/// </summary>
		public const int ColumnActionId = 0;

		/// <summary>
		/// 
		/// </summary>
		public const int ColumnDisplayName = 1;

		/// <summary>
		/// 
		/// </summary>
		public const int ColumnDescription = 2;

		/// <summary>
		/// 
		/// </summary>
		public const int ColumnGroupName = 3;

		/// <summary>
		/// Create a new instance of ActionTable.
		/// </summary>
		protected override System.Data.DataTable CreateInstance() {
			return new ActionTable();
		}

		/// <summary>
		/// Create as new instance of ActionRow.
		/// </summary>
		/// <param name="builder">Builder for data row.</param>
		protected override System.Data.DataRow NewRowFromBuilder(System.Data.DataRowBuilder builder) {
			return new ActionRow(builder);
		}

		/// <summary>
		/// Count of rows.
		/// </summary>
		public int Count {
			get {
				return base.Rows.Count;
			}
		}

		/// <summary>
		/// Get row.
		/// </summary>
		/// <param name="index">Index of row.</param>
		public ActionRow this[int index] {
			get {
				return (ActionRow)(base.Rows[index]);
			}
			set {
				throw new System.NotImplementedException();
			}
		}

		/// <summary>
		/// Add a row.
		/// </summary>
		/// <param name="row" >Row which will be added.</param>
		public void AddRow(ActionRow row) {
			base.Rows.Add(row);
		}

		/// <summary>
		/// Remove a row.
		/// </summary>
		/// <param name="row" >Row which will be removed.</param>
		public void RemoveRow(ActionRow row) {
			base.Rows.Remove(row);
		}

		/// <summary>
		/// Add a new row with default values.
		/// </summary>
		public ActionRow AddNewRow() {
			var row = base.NewRow() as ActionRow;

			System.Diagnostics.Debug.Assert(row != null);

			base.Rows.Add(row);

			return row;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="actionId"></param>
		/// <param name="displayName"></param>
		/// <param name="description"></param>
		/// <param name="groupName"></param>
		/// <returns></returns>
		public ActionRow AddNewRow(int actionId, string displayName, string description, string groupName) {
			var row = base.NewRow() as ActionRow;

			System.Diagnostics.Debug.Assert(row != null);

			row.ActionId = actionId;
			row.DisplayName = displayName;
			row.Description = description;
			row.GroupName = groupName;

			base.Rows.Add(row);

			return row;
		}

		/// <summary>
		/// Type of data row.
		/// </summary>
		protected override System.Type GetRowType() {
			return typeof(ActionRow);
		}

		#region Members of System.Collections.Generic.IEnumerable<ActionRow>.

		/// <summary>
		/// 
		/// </summary>
		/// <param name="item"></param>
		/// <returns></returns>
		public int IndexOf(ActionRow item) {
			return base.Rows.IndexOf(item);
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="index" ></param>
		/// <param name="item" ></param>
		public void Insert(int index, ActionRow item) {
			base.Rows.InsertAt(item, index);
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="index"></param>
		public void RemoveAt(int index) {
			base.Rows.RemoveAt(index);
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="item" ></param>
		public void Add(ActionRow item) {
			base.Rows.Add(item);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="item" ></param>
		/// <returns></returns>
		bool System.Collections.Generic.ICollection<ActionRow>.Contains(ActionRow item) {
			throw new System.NotImplementedException();
		}

		/// <summary>
		///
		/// </summary>
		/// <param name="keys" ></param>
		/// <returns></returns>
		public bool ContainsKey(params object[] keys) {
			System.Diagnostics.Debug.Assert(keys != null);
			System.Diagnostics.Debug.Assert(keys.Length != 0);

			return base.Rows.Contains(keys);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="array" ></param>
		/// <param name="arrayIndex" ></param>
		public void CopyTo(ActionRow[] array, int arrayIndex) {
			base.Rows.CopyTo(array, arrayIndex);
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsReadOnly {
			get {
				return false;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="item" ></param>
		/// <returns></returns>
		public bool Remove(ActionRow item) {
			base.Rows.Remove(item);
			return true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns ></returns>
		public IEnumerator<ActionRow> GetEnumerator() {
			var iter = base.Rows.GetEnumerator();
			while (iter.MoveNext()) {
				yield return iter.Current as ActionRow;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns ></returns>
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
			return base.Rows.GetEnumerator();
		}

		#endregion

		/// <summary>
		/// Initialize data table.
		/// </summary>
		public ActionTable() {
			this.TableName = "ActionTable";
			this.BeginInit();
			var column0 = new System.Data.DataColumn("actionId", typeof(int), null, System.Data.MappingType.Attribute);
			column0.Unique = true;
			base.Columns.Add(column0);
			var column1 = new System.Data.DataColumn("displayName", typeof(string), null, System.Data.MappingType.Attribute);
			column1.DefaultValue = "";
			base.Columns.Add(column1);
			var column2 = new System.Data.DataColumn("description", typeof(string), null, System.Data.MappingType.Attribute);
			column2.DefaultValue = "";
			base.Columns.Add(column2);
			var column3 = new System.Data.DataColumn("groupName", typeof(string), null, System.Data.MappingType.Attribute);
			column3.DefaultValue = "";
			base.Columns.Add(column3);
			base.PrimaryKey = new System.Data.DataColumn[] 
            { 
                column0 
            };
			this.EndInit();
		}
	} // end of ActionTable.

	/// <summary>
	/// ActionRow.
	/// </summary>
	[System.ComponentModel.DesignerCategory("Code")]
	public class ActionRow : System.Data.DataRow {
		/// <summary>
		/// Initialize data row.
		/// </summary>
		public ActionRow(System.Data.DataRowBuilder builder)
			: base(builder) {
		}

		/// <summary>
		/// 
		/// </summary>
		public int ActionId {
			get {
				var col = base.Table.Columns[ActionTable.ColumnActionId];

				if (base.IsNull(col)) {
					throw new System.NullReferenceException("Value of ActionTable[\"actionId\"] is null!");
				} else {
					return (int)(base[col]);
				}
			}
			set {
				var col = base.Table.Columns[ActionTable.ColumnActionId];

				base[col] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void SetActionIdNull() {
			var col = base.Table.Columns[ActionTable.ColumnActionId];

			base[col] = System.Convert.DBNull;
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsActionIdNull {
			get {
				var col = base.Table.Columns[ActionTable.ColumnActionId];

				return base.IsNull(col);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string DisplayName {
			get {
				var col = base.Table.Columns[ActionTable.ColumnDisplayName];

				if (base.IsNull(col)) {
					throw new System.NullReferenceException("Value of ActionTable[\"displayName\"] is null!");
				} else {
					return (string)(base[col]);
				}
			}
			set {
				var col = base.Table.Columns[ActionTable.ColumnDisplayName];

				base[col] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void SetDisplayNameNull() {
			var col = base.Table.Columns[ActionTable.ColumnDisplayName];

			base[col] = System.Convert.DBNull;
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsDisplayNameNull {
			get {
				var col = base.Table.Columns[ActionTable.ColumnDisplayName];

				return base.IsNull(col);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string Description {
			get {
				var col = base.Table.Columns[ActionTable.ColumnDescription];

				if (base.IsNull(col)) {
					throw new System.NullReferenceException("Value of ActionTable[\"description\"] is null!");
				} else {
					return (string)(base[col]);
				}
			}
			set {
				var col = base.Table.Columns[ActionTable.ColumnDescription];

				base[col] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void SetDescriptionNull() {
			var col = base.Table.Columns[ActionTable.ColumnDescription];

			base[col] = System.Convert.DBNull;
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsDescriptionNull {
			get {
				var col = base.Table.Columns[ActionTable.ColumnDescription];

				return base.IsNull(col);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public string GroupName {
			get {
				var col = base.Table.Columns[ActionTable.ColumnGroupName];

				if (base.IsNull(col)) {
					throw new System.NullReferenceException("Value of ActionTable[\"groupName\"] is null!");
				} else {
					return (string)(base[col]);
				}
			}
			set {
				var col = base.Table.Columns[ActionTable.ColumnGroupName];

				base[col] = value;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public void SetGroupNameNull() {
			var col = base.Table.Columns[ActionTable.ColumnGroupName];

			base[col] = System.Convert.DBNull;
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsGroupNameNull {
			get {
				var col = base.Table.Columns[ActionTable.ColumnGroupName];

				return base.IsNull(col);
			}
		}
	} // end of ActionRow.
}
